package com.magic.smartcity.utils;

import android.util.Log;

import java.io.IOException;

import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ActivityComponent;
import dagger.hilt.android.components.FragmentComponent;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @description:已经做动态Token处理，让每个接口将无需手动传入Token值
 * @create: Create by Magic on 2022/2/4.
 */
@InstallIn({FragmentComponent.class, ActivityComponent.class})
@Module
public class HttpClient {
    private static Retrofit mRetrofit = null;
    private static OkHttpClient okHttpClient = null;

    @Provides
    public Retrofit getRetrofit() {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient()
                    .newBuilder()
                    .addInterceptor(new TokenHeaderInterceptor())//当用户登录后，统一添加Token
                    .build();
        }
        if (mRetrofit == null) {
            mRetrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(Constant.Service_IP)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                    .build();
        }
        return mRetrofit;
    }

//
//    public void newCallToEnqueue(Request request, OnGetResult resultInterface){
//        mOkHttpClient.newCall(request).enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                e.printStackTrace();
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                StringBuffer result = new StringBuffer();
//                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
//                ResponseBody body = response.body();
//                if (body.contentLength() > 1024 * 1024) {
//                    InputStream inputStream = body.byteStream();
//                    BufferedReader bufferedReader;
//                    bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//                    String line;
//                    while ((line = bufferedReader.readLine()) != null) {
//                        result.append(line);
//                    }
//                } else {
//                    result.append(body.string());
//                }
//                resultInterface.getResult(result.toString());
//
//            }
//        });
//    }
//    public interface OnGetResult{
//        void getResult(String json);
//    }

}

class TokenHeaderInterceptor implements Interceptor {

    private static final String TAG = "TokenHeaderInterceptor";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if (Constant.TOKEN != null) {
            Log.d(TAG,"====当前拦截器,Token"+(Constant.TOKEN!=null?"存在":"不存在"+"===="));
            request = request.newBuilder().header("Authorization", Constant.TOKEN).build();
        }
        return chain.proceed(request);
    }
}